//
//  YTTCityGroupViewController.m
//  跑圈儿
//
//  Created by dllo on 16/4/28.
//  Copyright © 2016年 唐康. All rights reserved.
//

#import "YTTCityGroupViewController.h"
#import "YTTCityGroup.h"
#import <RESideMenu.h>
#import "YTTDataTool.h"







@interface YTTCityGroupViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSArray *cityGroupArray;
@property(nonatomic, strong) UITableView *tableView;

@end

@implementation YTTCityGroupViewController



- (NSArray *)cityGroupArray {
    
    if (!_cityGroupArray) {
        _cityGroupArray = [YTTDataTool getAllCityGroups];
    }
    
    return _cityGroupArray;
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
   
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    self.navigationItem.title = @"城市列表";
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回"style:UIBarButtonItemStyleDone target:self action:@selector(clickBackItem)];
    self.navigationItem.leftBarButtonItem = backItem;
    [self createTableView];
    
}

- (void)clickBackItem {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


-(void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:YTTBOUNDS style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableCell"];
    
}





-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.cityGroupArray.count;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    YTTCityGroup *cityGroup = self.cityGroupArray[section];
    return cityGroup.cities.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableCell" forIndexPath:indexPath];
    
    YTTCityGroup *cityGroup = self.cityGroupArray[indexPath.section];
    cell.textLabel.text = cityGroup.cities[indexPath.row];
    
    
    return cell;
    
    
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    YTTCityGroup *cityGroup = self.cityGroupArray[section];
    return cityGroup.title;
}

//返回talbeViewindex数组
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    
    return [self.cityGroupArray valueForKeyPath:@"title"];
}


//选中哪一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    YTTCityGroup *cityGroup = self.cityGroupArray[indexPath.section];
    NSString *cityName = cityGroup.cities[indexPath.row];
    
    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"DidChangeCity" object:self userInfo:@{@"cityName":cityName}];
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
